import * as React from 'react';
import dayjs from 'dayjs';

const {
    useState,
    useEffect
} = React;

function useNumberState(value: number): [number, (n: any) => void] {
    const [n, setN] = useState(value);

    function setNumberHook(n: any) {
        setN(+n); // 强制转化
    }

    return [n, setNumberHook];
}

export default function useTimeController(time:dayjs.Dayjs) {
    const [year, setYear] = useNumberState(time.year());
    const [month, setMonth] = useNumberState(time.month() + 1);
    const [date, setDate] = useNumberState(time.date());
    const [hour, setHour] = useNumberState(time.hour());
    const [minute, setMinute] = useNumberState(time.minute());
    function getTime() {
        return dayjs(new Date(year, month - 1, date, hour, minute)).unix()*1000;
    }
    return {
        year, setYear,
        month, setMonth,
        date, setDate,
        hour, setHour,
        minute, setMinute,
        getTime
    }

}